<script setup lang="ts">
import { message } from "@/utils/message";
import { ref } from "vue";
import ReCol from "@/components/ReCol";
import { formRules } from "./utils/rule";
import { FormProps } from "./utils/types";
import { EditPen } from "@element-plus/icons-vue";

import routePermissions from "./routePermissions/index.vue";
import elementPermissions from "./elementPermissions/index.vue";

const props = withDefaults(defineProps<FormProps>(), {
  formInline: () => ({
    id: "",
    name: "",
    auth_pages: "",
    auth_element: "",
    is_caller: "0",
    remark: ""
  })
});

const ruleFormRef = ref();
const newFormInline = ref(props.formInline);

const isCallOption = [
  {
    value: "0",
    label: "不参与"
  },
  {
    value: "1",
    label: "参与"
  }
];

// 选择路由权限
const routePermissionsRef = ref();
function authPagesSelectClick(auth_pages) {
  routePermissionsRef.value.openDrawer(auth_pages);
}

// 修改路由权限
function saveRoutePermissions(authPageRouter) {
  newFormInline.value.auth_pages = JSON.stringify(authPageRouter)
}

// 选择元素权限
const elementPermissionsRef = ref();
function authElementSelectClick(auth_element, auth_pages) {
  if (!auth_pages) return message("请先设置路由权限", { type: "error" });
  elementPermissionsRef.value.openDrawer(auth_element, auth_pages);
}

// 修改元素权限
function saveElementPermissions(authElementRouter) {
  newFormInline.value.auth_element = JSON.stringify(authElementRouter)
}
function getRef() {
  return ruleFormRef.value;
}

defineExpose({ getRef });
</script>

<template>
  <div>
    <el-form
      ref="ruleFormRef"
      :model="newFormInline"
      :rules="formRules"
      label-width="120px"
    >
      <el-row :gutter="30">
        <re-col :value="24" :xs="24" :sm="24">
          <el-form-item label="角色名称:" prop="name">
            <el-input
              v-model="newFormInline.name"
              clearable
              maxlength="50"
              placeholder="请输入"
              show-word-limit
            />
          </el-form-item>
        </re-col>
        <re-col :value="24" :xs="24" :sm="24">
          <el-form-item label="路由权限:" prop="auth_pages">
            <template v-if="newFormInline.auth_pages">
              <el-input
                class="doneInput"
                value="编辑设置"
                :suffix-icon="EditPen"
                readonly
                @click="authPagesSelectClick(newFormInline.auth_pages)"
              />
            </template>
            <template v-else>
              <el-input
                class="readonlyInput"
                placeholder="未设置，去设置"
                :suffix-icon="EditPen"
                readonly
                @click="authPagesSelectClick(newFormInline.auth_pages)"
              />
            </template>
          </el-form-item>
        </re-col>
        <re-col :value="24" :xs="24" :sm="24">
          <el-form-item label="元素权限:" prop="auth_element">
            <template v-if="newFormInline.auth_element">
              <el-input
                class="doneInput"
                value="编辑设置"
                :suffix-icon="EditPen"
                readonly
                @click="authElementSelectClick(newFormInline.auth_element, newFormInline.auth_pages)"
              />
            </template>
            <template v-else>
              <el-input
                class="readonlyInput"
                placeholder="未设置，去设置"
                :suffix-icon="EditPen"
                readonly
                @click="authElementSelectClick(newFormInline.auth_element, newFormInline.auth_pages)"
              />
            </template>
          </el-form-item>
        </re-col>
        <re-col :value="24" :xs="24" :sm="24">
          <el-form-item label="是否参与群呼:" prop="is_caller">
            <el-radio-group v-model="newFormInline.is_caller">
              <el-radio
                v-for="(item, index) in isCallOption"
                :key="index"
                :value="item.value"
                >{{ item.label }}</el-radio
              >
            </el-radio-group>
          </el-form-item>
        </re-col>
        <re-col :value="24" :xs="24" :sm="24">
          <el-form-item label="备注:">
            <el-input
              v-model="newFormInline.remark"
              clearable
              maxlength="200"
              placeholder="请输入"
              show-word-limit
              type="textarea"
            />
          </el-form-item>
        </re-col>
      </el-row>
    </el-form>

    <routePermissions ref="routePermissionsRef" @save="saveRoutePermissions"></routePermissions>

    <elementPermissions ref="elementPermissionsRef" @save="saveElementPermissions"></elementPermissions>

  </div>
</template>

<style lang="scss" scoped>
:deep(.readonlyInput .el-input__wrapper) {
  background-color: #efefef;
}
:deep(.readonlyInput .el-input__inner) {
  cursor: pointer;
}
:deep(.doneInput .el-input__wrapper) {
  background-color: #EBFDFD;
}
:deep(.doneInput .el-input__inner) {
  cursor: pointer;
  color:#60D1D1;
}
</style>
